package newfile

import (
	"cuex/pkg/root"
	"cuex/pkg/utils/flag"
	"cuex/pkg/vvlog"
	"cuex/samples/newfile/pkg/res"
	"fmt"
	"github.com/spf13/cobra"
	"os"
	"text/template"
)

var (
	logger     = vvlog.NewLogger("newfile.go")
	newFileCmd = &cobra.Command{
		Use:   "newfile",
		Short: "newfile demo",
		RunE: func(c *cobra.Command, args []string) error {
			flag.PrintFlags(c.Flags())
			logger.Info("to do ")
			return runc()
		},
	}
)

func init() {
	//servCmd.PersistentFlags().StringVar(&sslKey, "key", "", "serv's ssl key")
	root.AddCmd(newFileCmd)
}

func runc() error {
	t := template.New("test")
	//tplBindata,_:= tpl.Asset("res/test.json.tpl")
	t, err := t.Parse(res.Source)
	if err != nil {
		logger.Error(err)
		return err
	} else {
		outputFile, err := os.Create("test.json")
		if err != nil {
			return err
		}
		defer func() { _ = outputFile.Close() }()
		err = t.Execute(outputFile, "123456")
		if err != nil {
			fmt.Println(err)
			return err
		}
	}
	return nil
}
